﻿using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SPListTemplateManager {
    public partial class Form2 : Form {
        public Form2() {
            InitializeComponent();

            Helpers.GetAllWebApplications(false);

            if (Helpers.webAppsList.Count > 0) {
                Helpers.FillWebAppComboBox(comboBox1);
            } else {
                MessageBox.Show("No web application was found!");
            }
        }

        SPWeb globalWeb = null;
        SPList selectedList = null;

        // web apps change --> get site colls
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
            comboBox2.Items.Clear();
            comboBox3.Items.Clear();
            comboBox4.Items.Clear();
            globalWeb = null;
            selectedList = null;
            
            Cursor.Current = Cursors.WaitCursor;
            SPWebApplication selectedApp = (SPWebApplication)comboBox1.SelectedItem;

            foreach (SPWebApplication webApp in Helpers.webAppsList) {
                if (webApp == selectedApp) {
                    foreach (SPSite item in webApp.Sites) {
                        comboBox2.Items.Add(item.Url);
                    }
                    break;
                }
            }
        }

        // site colls change --> get sites
        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e) {
            comboBox3.Items.Clear();
            comboBox4.Items.Clear();
            selectedList = null;
            Cursor.Current = Cursors.WaitCursor;
            string selectedSiteColl = (string)comboBox2.SelectedItem;

            using (SPSite site = new SPSite(selectedSiteColl)) {
                SPWebCollection webColl = site.AllWebs;
                foreach (SPWeb web in webColl) {
                    comboBox3.Items.Add(web.Url);
                }
            }
        }

        // site change --> get lists        
        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e) {
            Cursor.Current = Cursors.WaitCursor;
            string siteUrl = (string)comboBox3.SelectedItem;

            using (SPSite site = new SPSite(siteUrl)) {
                using (SPWeb web = site.OpenWeb()) {
                    globalWeb = web;
                    SPListCollection listColl = web.Lists;

                    foreach (SPList list in listColl) {
                        comboBox4.Items.Add(list);
                    }
                }
            }
        }

        

        private void comboBox4_SelectedIndexChanged(object sender, EventArgs e) {
            selectedList = (SPList)comboBox4.SelectedItem;
        }

        private void btnCancel_Click(object sender, EventArgs e) {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e) {
            if (selectedList != null) {
                if (!string.IsNullOrEmpty(textBox1.Text) || !string.IsNullOrEmpty(textBox2.Text)) {
                    Cursor.Current = Cursors.WaitCursor;
                    string selectedSiteColl = (string)comboBox2.SelectedItem;
                    try {
                        selectedList.SaveAsTemplate(textBox1.Text + ".stp", textBox2.Text, textBox3.Text, checkBox1.Checked);
                        MessageBox.Show("List template is saved to template gallery!");
                    } catch (Exception ex) {
                        MessageBox.Show("ERROR: " + ex.Message);
                    }
                } else {
                    MessageBox.Show("You must enter file name and title for the template!");
                }
            } else {
                MessageBox.Show("You haven't selected a list!");
            }
        }

    }
}
